<?php

namespace app\admin\controller;

use app\common\BaseHelper as Helper;
use app\common\Common;
use app\admin\model\Store;
use app\admin\model\Storerole;

use think\Request;

class Storestaff extends Base
{

    public function _initialize()
    {
        parent::_initialize();

        $storeId = $this->request->param('store_id');
        $defaultWhere = $this->getDefaultWhere();
        $defaultWhere['store_id'] = $storeId;
        $this->defaultWhere = $defaultWhere;
        $this->defaultOrder = 'create_date DESC';

        $this->redirect = url('admin/' . strtolower($this->model) . '/index', ['store_id' => $storeId]);

        $this->assign('store_id', $storeId);

        if($this->request->action() == 'add' || $this->request->action() == 'edit'){
            $roles = Storerole::where(['status' => 1])->select();

            $this->assign('roles', $roles);
        }
    }

    public function index(){

        return parent::index();
    }

    /**
     * Add
     */
    public function add(Request $request){

        return parent::add($request);
    }

    /**
     * Edit
     */
    public function edit(Request $request){

        return parent::edit($request);
    }

    /**
     * Save Post
     */
    public function savePost(Request $request){
        $model = model($this->model);

        if ($request->isPost()) {
            $data = $request->param();
            if($data['cashier_pwd']){
                $data['cashier_pwd'] = Helper::passwordEncrypt($data['cashier_pwd']);
            }else{
                unset($data['cashier_pwd']);
            }

            if ($data['id']) {
                // Update data
                $result = $model->allowField(true)->save($data, ['id' => $data['id']]);
                $logInfo = $this->admin->name . '更新了一条' . lang($this->model) . '数据。';
                $afterAction = '_after_update';
            } else {
                // Insert data
                $data['id'] = Helper::getUUID();
                $data['create_date'] = date('Y-m-d H:i:s');
                $result = $model->save($data);
                $logInfo = $this->admin->name . '添加了一条' . lang($this->model) . '数据。';
                $afterAction = '_after_insert';
            }

            if($result !== false) {
                // Query执行后的操作
                $model->$afterAction($data);

                // 写入日志
                Common::adminLog($request, $logInfo);

                return $this->success('保存成功！', $this->redirect);
            } else {
                return $this->error($model->getError());
            }
        } else {
            return $this->error('提交方式错误！');
        }
    }

    /**
     * 处理列表数据
     */
    public function handleListData($data){
        foreach ($data as $item) {
            $item['store_id'] = Store::where('id', $item['store_id'])->value('name');
            $item['role_id'] = Storerole::where('id', $item['role_id'])->value('name');
        }

        return $data;
    }

    /**
     * 筛选条件
     */
    public function getFilterWhere($request){
        $param = $request->param();
        $where = [];
        if(isset($param['name']) && $param['name']){
            $where['name'] = ['like', '%'.$param['name'].'%'];
        }

        return $where;
    }

}